# @Author: dpf
# @Date  : 2024/12/02/16:51
from entity.BookTypeModel import BookType
from util import dbUtil


def add(bookType: BookType):
    con = None
    try:
        con = dbUtil.getCon()
        cursor = con.cursor()
        cursor.execute(f"insert into book_type values (null,'{bookType.bookTypeName}','{bookType.bookTypeDesc}') ")
        return cursor.rowcount
    except Exception as e:
        print(e)
        con.rollback()
        return 0
    finally:
        dbUtil.closeCon(con)


def list(s_bookTypeName: str):
    con = None
    try:
        con = dbUtil.getCon()
        cursor = con.cursor()
        sql = " select * from book_type where 1=1 "
        if s_bookTypeName.strip() != '':
            sql += " and bookTypeName like '%" + s_bookTypeName + "%' "
        print(sql)
        cursor.execute(sql)
        return cursor.fetchall()
    except Exception as e:
        print(e)
        con.rollback()
        return None
    finally:
        dbUtil.closeCon(con)


def update(bookType: BookType):
    con = None
    try:
        con = dbUtil.getCon()
        cursor = con.cursor()
        cursor.execute(
            f" update book_type set bookTypeName='{bookType.bookTypeName}',bookTypeDesc = '{bookType.bookTypeDesc}' where id = {bookType.id} ")
        return cursor.rowcount
    except Exception as e:
        print(e)
        con.rollback()
        return 0
    finally:
        dbUtil.closeCon(con)


def delete(id):
    con = None
    try:
        con = dbUtil.getCon()
        cursor = con.cursor()
        cursor.execute(f" delete from  book_type  where id = {id} ")
        return cursor.rowcount
    except Exception as e:
        print(e)
        con.rollback()
        return 0
    finally:
        dbUtil.closeCon(con)
